package com.dwj.auto.comment.service.impl;

import com.dwj.auto.comment.common.response.PageResponse;
import com.dwj.auto.comment.common.response.Response;
import com.dwj.auto.comment.common.utils.CodeUtil;
import com.dwj.auto.comment.config.mybatis.plus.CommonServiceImpl;
import com.dwj.auto.comment.dao.entity.*;
import com.dwj.auto.comment.dao.mapper.*;
import com.dwj.auto.comment.model.form.ExamForm;
import com.dwj.auto.comment.model.req.ExamCriteria;
import com.dwj.auto.comment.model.req.ExamSearchCriteria;
import com.dwj.auto.comment.model.vo.ExamSearchVo;
import com.dwj.auto.comment.model.vo.ExamVo;
import com.dwj.auto.comment.service.IExamService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dwjian
 * @since 2020-01-21
 */
@Service
public class ExamServiceImpl extends CommonServiceImpl<ExamMapper, Exam> implements IExamService {

    @Resource
    private StuClassMapper stuClassMapper;
    @Resource
    private CourseMapper courseMapper;
    @Resource
    private UserRoleMapper userRoleMapper;
    @Resource
    private UserMapper userMapper;

    @Override
    public PageResponse<ExamVo> getList(ExamCriteria criteria) {
        return PageResponse.success(this.voPage(criteria));
    }

    @Override
    public PageResponse<ExamSearchVo> searchList(ExamSearchCriteria criteria) {
        return PageResponse.success(this.voPage(criteria));
    }

    @Override
    public Response<String> edit(ExamForm form) {
        StuClass stuClass = stuClassMapper.selectById(form.getClassId());
        if(Objects.isNull(stuClass)){
            return Response.fail("班级信息不存在");
        }
        Course course = courseMapper.selectById(form.getCourseId());
        if(Objects.isNull(course)){
            return Response.fail("科目信息不存在");
        }
        UserRole userRole = userRoleMapper.selectById(form.getInvigilatorId());
        if(Objects.isNull(userRole)){
            return Response.fail("监考老师不存在");
        }
        User user = userMapper.selectById(userRole.getUserId());
        if(Objects.isNull(user)){
            return Response.fail("监考老师信息错误");
        }
        if(StringUtils.isEmpty(form.getId())){
            return add(form, stuClass, course, user);
        }
        Exam exam = new Exam();
        BeanUtils.copyProperties(form, exam);
        exam.setClassName(stuClass.getClassName());
        exam.setCourseName(course.getName());
        exam.setInvigilator(user.getRealName());
        boolean update = this.updateById(exam);
        if(update) {
            return Response.success();
        }
        return Response.fail("更新失败");
    }

    /**
     * 新增考试
     * @param form
     * @param stuClass
     * @param course
     * @param user
     * @return
     */
    private Response<String> add(ExamForm form, StuClass stuClass, Course course, User user) {
        Exam exam = new Exam();
        BeanUtils.copyProperties(form, exam);
        exam.setId(CodeUtil.uuid());
        exam.setClassName(stuClass.getClassName());
        exam.setCourseName(course.getName());
        exam.setInvigilator(user.getRealName());
        boolean add = this.save(exam);
        if(add) {
            return Response.success();
        }
        return Response.fail("新增失败");
    }
}
